
from dataclasses import field
from pkg.response import HttpCode
from typing import Any


class CustomException(Exception):
    code: HttpCode.FAIL
    msg: str
    # 这里的意思是,每次实例化的时候，feild会执行，
    # default_factory表示执行的是生产默认值的操作，
    # 后面跟的dict表示生成的默认值调用的函数是dict这个函数，
    # dict每次执行都会生成一个全新的空对象
    # 这样每次实例化的时候都是全新的空对象
    data: Any = field(default_factory=dict)

    def __init__(self, message: str = None, data: Any = None):
        super().__init__()
        self.msg = message
        self.data = data


class FailException(CustomException):
    """通用失败异常"""
    pass


class NotFoundException(CustomException):
    """未找到数据异常"""
    code = HttpCode.NOT_FOUND


class UnauthorizedException(CustomException):
    """未授权异常"""
    code = HttpCode.UNAUTHORIZED


class ForbiddenException(CustomException):
    """无权限异常"""
    code = HttpCode.FORBIDDEN


class ValidateErrorException(CustomException):
    """数据验证异常"""
    code = HttpCode.VALIDATE_ERROR